#ifndef __LIB_FILE_UTILS_H__
#define __LIB_FILE_UTILS_H__

#include "libutilsbase.h"
#if defined(WIN32)
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif

LIB_NS_BEGIN
typedef bool ProcessFileFun(const TCHAR* szFileName,void* pdata);



class LIBAPI UFileSystem
{
public:
	static tstring GetFileName(const TCHAR* pPath);
	static tstring GetAppPath();
	
	static bool IsDirSep(TCHAR ch);
	static bool IsFileExist( const TCHAR* name );
	static bool DeleteFile( const TCHAR* filename );
	static bool IsSameContext(const TCHAR* firstfile,const TCHAR* secondefile);
	
	
	static bool Rename( const TCHAR* oldname,const TCHAR* newname );
	static int64 GetFileSize( const char* filefullpath );
	static int64 GetFileSize( FILE* fp );
#ifdef WIN32
	static int64 GetFileSize( HANDLE hFile );
	static bool	CloseAllFiles();
#endif
private:
	UFileSystem();
};


class LIBAPI UFileUtils
{
public:
    enum opttime
    {
        acess,
        create,
        modify
    };

	static FILE* FromFileHandle(HANDLE hFile,const TCHAR* ope);
    static bool CopyFile(const tstring& src, const tstring& dest,bool LeaveoldFile = true);
    static tstring ReadString(const TCHAR* fname);
    static void Write(const TCHAR* fname, const TCHAR* content,	bool isappend=false);
	time_t GetFileDate(const TCHAR *pFileName, int32 pMode );
	static bool MoveFile(const TCHAR *poldname, const TCHAR *pnewname);


	static bool ReadLines(std::vector<std::string>* pvec,const char* fname );
	static bool ReadLines(std::vector<std::wstring>*pvec,const TCHAR* fname );

	void WriteContent( const TCHAR* file, const TCHAR* content, bool isappend );

};

template<typename T>
void WriteFile(const TCHAR* fname,const T& content, bool isappend=true)
{
	std::ios_base::open_mode mode = std::ofstream::out;
	mode = isappend  ? (mode| std::ofstream::app) : mode;    
	std::ofstream outfile(fname, mode);  
	outfile << content<<std::endl;
}

LIB_NS_END
#endif